package com.hupu.jrs.ui.userprofile

import android.app.Fragment
import android.app.FragmentManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.support.design.widget.AppBarLayout
import android.support.design.widget.CollapsingToolbarLayout
import android.support.design.widget.CoordinatorLayout
import android.support.design.widget.TabLayout
import android.support.v13.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.Toolbar
import android.text.TextUtils
import android.widget.ImageView
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.facebook.drawee.view.SimpleDraweeView
import java.util.ArrayList
import javax.inject.Inject
import butterknife.BindView
import butterknife.ButterKnife
import com.hupu.jrs.R
import com.hupu.jrs.bean.UserResult
import com.hupu.jrs.ui.BaseSwipeBackActivity
import com.hupu.jrs.ui.browser.BrowserFragment

/**
 * Created by sll on 2016/3/11.
 * 个人中心
 */
class UserProfileActivity : BaseSwipeBackActivity(), UserProfileContract.View, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.ivCover)
    internal var ivCover: ImageView? = null
    @BindView(R.id.ivPhoto)
    internal var ivPhoto: SimpleDraweeView? = null
    @BindView(R.id.toolbar)
    internal var toolbar: Toolbar? = null
    @BindView(R.id.tabs)
    internal var tabs: TabLayout? = null
    @BindView(R.id.toolbarLayout)
    internal var toolbarLayout: CollapsingToolbarLayout? = null
    @BindView(R.id.appbar)
    internal var appbar: AppBarLayout? = null
    @BindView(R.id.viewPager)
    internal var viewPager: ViewPager? = null
    @BindView(R.id.maincontent)
    internal var maincontent: CoordinatorLayout? = null
    @BindView(R.id.ivGender)
    internal var ivGender: ImageView? = null
    @BindView(R.id.tvRegisterTime)
    internal var tvRegisterTime: TextView? = null

    @Inject
    internal var mPresenter: UserProfilePresenter? = null

    private var mDialog: MaterialDialog? = null

    protected override val isApplyStatusBarTranslucency: Boolean
        get() = true

    protected override val isApplyStatusBarColor: Boolean
        get() = false

    override fun initContentView(): Int {
        return R.layout.activity_profile
    }

    override fun initInjector() {
        val uid = getIntent().getStringExtra("uid")
        DaggerUserProfileComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule())
                .userProfileModule(UserProfileModule(uid))
                .build()
                .inject(this)
    }

    override fun initUiAndListener() {
        ButterKnife.bind(this)
        mPresenter!!.attachView(this)
        initToolBar(toolbar)
        toolbarLayout!!.isTitleEnabled = false
        mDialog = MaterialDialog.Builder(this).title("提示")
                .content("正在加载")
                .cancelable(false)
                .progress(true, 0)
                .build()

        mPresenter!!.receiveUserInfo()
    }

    override fun renderUserData(userResult: UserResult?) {
        if (userResult != null) {
            setupViewPager(userResult)
            if (!TextUtils.isEmpty(userResult!!.header)) {
                ivPhoto!!.setImageURI(Uri.parse(userResult!!.header))
            }
            ivGender!!.setImageResource(
                    if (userResult!!.gender === 0) R.drawable.list_male else R.drawable.list_female)
            tvRegisterTime!!.setText(userResult!!.reg_time_str)
            setTitle(userResult!!.nickname)
        }
    }

    private fun setupViewPager(userResult: UserResult) {
        val adapter = MyAdapter(getFragmentManager())
        adapter.addFragment(BrowserFragment.newInstance(userResult.bbs_msg_url, ""),
                String.format("发帖(%s)", userResult.bbs_msg_count))
        adapter.addFragment(BrowserFragment.newInstance(userResult.bbs_post_url, ""),
                String.format("回帖(%s)", userResult.bbs_post_count))
        viewPager!!.adapter = adapter
        tabs!!.setupWithViewPager(viewPager)
    }

    class MyAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
        private val mFragments = ArrayList<Fragment>()
        private val mFragmentTitles = ArrayList<String>()

        fun addFragment(fragment: Fragment, title: String) {
            mFragments.add(fragment)
            mFragmentTitles.add(title)
        }

        override fun getItem(position: Int): Fragment {
            return mFragments[position]
        }

        override fun getCount(): Int {
            return mFragments.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return mFragmentTitles[position]
        }
    }

    override fun showLoading() {
        if (!mDialog!!.isShowing && !isFinishing()) {
            mDialog!!.show()
        }
    }

    override fun hideLoading() {
        if (mDialog!!.isShowing && !isFinishing()) {
            mDialog!!.dismiss()
        }
    }

    override fun showError() {

    }

    protected fun onDestroy() {
        super.onDestroy()
        mPresenter!!.detachView()
    }

    override fun onRefresh() {

    }

    companion object {

        fun startActivity(mContext: Context, uid: String) {
            val intent = Intent(mContext, UserProfileActivity::class.java)
            intent.putExtra("uid", uid)
            mContext.startActivity(intent)
        }
    }
}
